// DateModel.m 
// Camera 
// 
// Created by 尚朋 on 2018/10/18. 
// Copyright © 2018年 wzh. All rights reserved. 
// 
// @class  DateModel
// abstract  <#摘要#>
// description  <#描述#>


#import "DateModel.h"

@implementation DateModel

- (void)setDate:(NSDate *)date{
    _date = date;
    NSDate *senddate=  _date;
    NSCalendar* calendar = [NSCalendar currentCalendar];
//    [calendar setFirstWeekday:2];
//    calendar.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:8*3600];
    NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitWeekOfYear|NSCalendarUnitYearForWeekOfYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:senddate];
    self.year = [components year];
    self.month = [components month];
    self.day = [components day];
    self.hour = [components hour];
    self.minute = [components minute];
    self.second = [components second];
    self.weekOfYear = [components weekOfYear];
    self.weekday = [components weekday];

    NSInteger year = self.year;
    NSInteger month = self.month;
    if ((year%100==0&&year%400==0) || (year%100!=0&&year%4==0)) {
        //闰年判定：年份是整百数时,能被400整除。或者能被4整除但不能被100整除。其余的年份都为平年。
        //闰年 判断当前年份对应的月份是否是2月 如果是二月 判断是否是闰月 如果不是 赋值 刷新
        if (month==2) {
            self.monthType = MonthTimeTypeLeapMonth;
        }else if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){
            self.monthType = MonthTimeTypeLongMonth;
        }else{
            self.monthType = MonthTimeTypeShortMonth;
        }
    }else{
        //非闰年 判断当前年份对应的月份是否是2月 如果是二月 判断是否是闰月 如果不是 赋值 刷新
        if (month==2) {
            self.monthType = MonthTimeTypeNotLeapMonth;
        }else if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){
            self.monthType = MonthTimeTypeLongMonth;
        }else{
            self.monthType = MonthTimeTypeShortMonth;
        }
    }
}


- (void)setWeekday:(NSInteger)weekday{
    _weekday = weekday;
    switch (_weekday) {
        case 1:
            self.weekdayString = @"周日";
            break;
        case 2:
            self.weekdayString = @"周一";
            break;
        case 3:
            self.weekdayString = @"周二";
            break;
        case 4:
            self.weekdayString = @"周三";
            break;
        case 5:
            self.weekdayString = @"周四";
            break;
        case 6:
            self.weekdayString = @"周五";
            break;
        case 7:
            self.weekdayString = @"周六";
            break;
        default:
            break;
    }
    
}









//根据传入的日期 获取当前一周的开始-结束
- (void)weekDateWithDate:(NSDate *)date{
    
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit|NSDayCalendarUnit
                                         fromDate:now];
    // 得到星期几// 1(星期天) 2(星期二) 3(星期三) 4(星期四) 5(星期五) 6(星期六) 7(星期天)
    NSInteger weekDay = [comp weekday];// 得到几号
    NSInteger day = [comp day];
    
    NSLog(@"weekDay:%ld   day:%ld",weekDay,day);
    // 计算当前日期和这周的星期一和星期天差的天数
    long firstDiff,lastDiff;
    if (weekDay == 1) {
        firstDiff = 1;
        lastDiff = 0;
    }else{
        firstDiff = [calendar firstWeekday] - weekDay;
        lastDiff = 9 - weekDay;
    }

    NSLog(@"firstDiff:%ld   lastDiff:%ld",firstDiff,lastDiff);
// 在当前日期(去掉了时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
    firstDayComp.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:8*3600];

    [firstDayComp setDay:day + firstDiff];
    NSDate *firstDayOfWeek= [calendar dateFromComponents:firstDayComp];

    NSDateComponents *lastDayComp = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
    lastDayComp.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:8*3600];
    [lastDayComp setDay:day + lastDiff];
    NSDate *lastDayOfWeek= [calendar dateFromComponents:lastDayComp];

    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSLog(@"星期一开始 %@",[formater stringFromDate:firstDayOfWeek]);
    NSLog(@"当前 %@",[formater stringFromDate:now]);
    NSLog(@"星期天结束 %@",[formater stringFromDate:lastDayOfWeek]);

    
    
}

+ (void)weekDateWithDate:(NSDate *)date
               todayDate:(NSDate *)todayDate
                 finished:(void (^)(NSArray * modelArray,DateModel * weekYearModel))finished{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitWeekOfYear|NSCalendarUnitYearForWeekOfYear|NSCalendarUnitHour|NSCalendarUnitMinute fromDate:date];
    // 得到星期几// 1(星期天) 2(星期二) 3(星期三) 4(星期四) 5(星期五) 6(星期六) 7(星期天)
    NSInteger weekDay = [comp weekday];// 得到几号
    NSInteger day = [comp day];
    
    NSInteger weekOfYear = [comp weekOfYear];
    NSInteger year = [comp year];
    NSInteger month = [comp month];
    
    NSLog(@"weekDay:%ld   day:%ld weekOfYear:%ld ",weekDay,day,weekOfYear);
    // 计算当前日期和这周的星期一和星期天差的天数
    long firstDiff,lastDiff;
    if (weekDay == 1) {
        firstDiff = 0;
        lastDiff = 0;
    }else{
        firstDiff = [calendar firstWeekday] - weekDay;
        lastDiff = 9 - weekDay;
    }
    
    NSLog(@"firstDiff:%ld   lastDiff:%ld",firstDiff,lastDiff);
    // 在当前日期(去掉了时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date];
    [firstDayComp setDay:day + firstDiff];
    NSDate *firstDayOfWeek= [calendar dateFromComponents:firstDayComp];
    
    NSDateComponents *lastDayComp = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date];
    [lastDayComp setDay:day + lastDiff];
    NSDate *lastDayOfWeek= [calendar dateFromComponents:lastDayComp];
    
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSLog(@"星期一开始 %@",[formater stringFromDate:firstDayOfWeek]);
    NSLog(@"当前 %@",[formater stringFromDate:date]);
    NSLog(@"星期天结束 %@",[formater stringFromDate:lastDayOfWeek]);

    NSTimeInterval  startTimeInter = [firstDayOfWeek timeIntervalSince1970];
    
    NSMutableArray * marray = [NSMutableArray array];
    
    DateModel * sixModel;
    for (int i = 0; i<7; i++) {
        NSTimeInterval current = startTimeInter + 60*60*24*i;
        NSDate * currentDate = [NSDate dateWithTimeIntervalSince1970:current];
        DateModel * model = [[DateModel alloc]init];
        model.date = currentDate;
        [marray addObject:model];
        NSLog(@"----%ld年%ld月%ld 第%ld周 ",model.year,model.month,model.day,model.weekOfYear);
        if ([self isSameDay:currentDate asDate:todayDate]) {
            NSLog(@"是同一天");
            model.isToday = YES;
        }
        if (i == 6) {
            sixModel = model;
        }
    }
    
    
    //第几周 以每周的最后一天算
    
    //返回  model数组  当前年份-周数 -->sixModel
    
    if (finished) {
        finished(marray,sixModel);
    }
    
}

+ (BOOL)isSameDay:(NSDate *)date asDate:(NSDate *)compareDate
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:date];
    NSDate *dateOne = [cal dateFromComponents:components];
    
    components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:compareDate];
    NSDate *dateTwo = [cal dateFromComponents:components];
    
    return [dateOne isEqualToDate:dateTwo];
}



@end
